//
//  FCImageChoiceShow.m
//  commonUse
//
//  Created by WJC on 2018/11/21.
//
#import "FCImageChoiceShow.h"
#import "FCTImgBLabelView.h"
#import "FCAlertViewController.h"
//#import "UIImage+NIMKit.h"
@interface FCImageChoiceShow()<UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate,TImgBLabelcloseBtnDelegate,TImgBLabelmageTapDelegate>

@end
@implementation FCImageChoiceShow
//-(void)setCount:(NSInteger)count{
//    _maxShowToCount=count;
//    [self refreshImage];
//}
-(instancetype)initWithFrame:(CGRect)frame andChoiceArr:(NSMutableArray *)imageArr andMaxShowPerCount:(NSInteger)count{
    if (self = [super initWithFrame:frame]) {
        self.maxShowToCount=count;
        if (count>0) {
            self.imgHeight = self.width / self.maxShowToCount;
        }else{
             self.imgHeight = self.width / 4;
        }
        self.addImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(0, 0, self.imgHeight, self.imgHeight) andImage:[UIImage imageNamed:[WJCDeployModel getDeployWithDeployKey:WJCDeployAddImage]?[WJCDeployModel getDeployWithDeployKey:WJCDeployAddImage]: @"but_addgroupmembers"] andName:nil andHiddenClose:YES];
        self.addImgView.label.hidden=YES;
        [self addSubview:_addImgView];
        if (self.height<self.imgHeight) {
            self.height=self.imgHeight+8;
        }
        self.addImgView.imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhotoClick:)];
        [self.addImgView.imageView addGestureRecognizer:tap];
        if (imageArr.count>0) {
            self.imageArr=[imageArr mutableCopy];
            [self refreshImage];
        }
    }
    return self;
}

-(void)refreshImage{
    [self removeChildViews];
    
    if (self.maxShowToCount>0) {
         self.imgHeight = self.width / self.maxShowToCount;
    }
    if (!self.addImgView) {
        self.addImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(0, 0, self.imgHeight-8, self.imgHeight-8) andImage:[UIImage imageNamed:[WJCDeployModel getDeployWithDeployKey:WJCDeployAddImage]?[WJCDeployModel getDeployWithDeployKey:WJCDeployAddImage]: @"but_addgroupmembers"] andName:nil andHiddenClose:YES];
        self.addImgView.label.hidden=YES;
        [self addSubview:_addImgView];
        self.addImgView.imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPhotoClick:)];
        [self.addImgView.imageView addGestureRecognizer:tap];
    }
   __block CGFloat x=0,y=0;
    [self.imageArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str;
        if (self.strArr.count>idx) {
            str =self.strArr[idx];
        }
        FCTImgBLabelView *ImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(x, y, self.imgHeight-8, self.imgHeight-8) andImage:obj andName:self.noneChoiceShowStr?self.noneChoiceShowStr:str?str:@"" andHiddenClose:!self.isShowCloseBtn];
        if (idx == self.choiceShowImgTag) {
            ImgView.label.text=self.choiceShowStr?self.choiceShowStr:str?str:@"封面";
        }
        [self addSubview:ImgView];
        ImgView.closeBtn.tag=idx;
        ImgView.imageView.tag=idx;
        ImgView.label.tag=idx;
        ImgView.ImgDelegate=self;
        ImgView.btnDelegate=self;
        ImgView.imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageShow:)];
        [ImgView.imageView addGestureRecognizer:tap];
        ImgView.btnDelegate=self;
        ImgView.label.hidden=!self.isShowImgTag;
        x+=self.imgHeight;
        if (x+self.imgHeight>self.width) {
            x=0;
            y+=self.imgHeight;
        }
        
    }];
    self.height=y+self.imgHeight;
    if (self.imageArr.count<self.maxChoiceImage||self.maxChoiceImage<1) {
        self.addImgView.x=x;
        self.addImgView.y=y;
        [self addSubview:self.addImgView];
    }
    
    if ([self.delegate respondsToSelector:@selector(FCImageChoiceImage:)]) {
      
        [self.delegate FCImageChoiceImage:self];
    }
}
-(void)refreshWithNoDelegateImage{
    [self removeChildViews];
    if (self.maxShowToCount>0) {
        self.imgHeight = self.width / self.maxShowToCount;
    }
    if (self.addImgView.height !=self.imgHeight) {
        self.addImgView.width =self.imgHeight;
        self.addImgView.height =self.imgHeight;
        self.addImgView.imageView.width =  self.imgHeight-10;
        self.addImgView.imageView.height =  self.imgHeight-10;
    }
    
    __block CGFloat x=0,y=0;
    [self.imageArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        FCTImgBLabelView *ImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(x, y, self.imgHeight, self.imgHeight) andImage:obj andName:self.noneChoiceShowStr?self.noneChoiceShowStr:@"设为封面" andHiddenClose:!self.isShowCloseBtn];
        if (idx == self.choiceShowImgTag) {
            ImgView.label.text=self.choiceShowStr?self.choiceShowStr:@"封面";
        }
        [self addSubview:ImgView];
        ImgView.closeBtn.tag=idx;
        ImgView.imageView.tag=idx;
        ImgView.label.tag=idx;
        ImgView.ImgDelegate=self;
        ImgView.btnDelegate=self;
        ImgView.imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageShow:)];
        [ImgView.imageView addGestureRecognizer:tap];
        ImgView.btnDelegate=self;
        ImgView.label.hidden=!self.isShowImgTag;
        x+=self.imgHeight;
        if (x+self.imgHeight>self.width) {
            x=0;
            y+=self.imgHeight;
        }
    }];
    self.height=y+self.imgHeight;
    if (self.imageArr.count<self.maxChoiceImage||self.maxChoiceImage<1) {
        self.addImgView.x=x;
        self.addImgView.y=y;
        [self addSubview:self.addImgView];
    }
}
-(void)imageShow:(UITapGestureRecognizer*)sender{
//    FCImageShowBrowser *browser=[FCImageShowBrowser shareBrowser];
//    browser.ImagesArr=self.imageArr;
//    browser.currentPage=sender.view.tag;
//    browser.clickImageView=(UIImageView*)sender.view;
//    browser.currentControllerView=[UIApplication sharedApplication].keyWindow;
//    [browser showView];
    
    UIViewController *controller =[FCCGetCurrentVcTool getCurrentViewController];

    [FCPhotoBrowserTool photoBrowserWithImages:self.imageArr sourceImageViews:nil currentIndex:sender.view.tag showFromVC:controller];
    
}
#pragma mark--imageChoice
- (void)addPhotoClick:(UITapGestureRecognizer*)sender {
    [self.viewController.view endEditing:YES];
    if(self.imageArr.count > self.maxChoiceImage&&self.maxChoiceImage>1){
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"图片或文件已超过%ld张,无法再选",self.maxChoiceImage]];
        return;
    }
    
    UIViewController *uc=[FCCGetCurrentVcTool getCurrentViewController];;
    if(!self.takePhoto){
        TZImagePickerController *pickerController = [[TZImagePickerController alloc]initWithMaxImagesCount:self.maxChoiceImage-self.imageArr.count delegate:self];
        pickerController.sortAscendingByModificationDate = NO;
        pickerController.naviBgColor = WJCARGBColor(245, 248, 252,1);
        pickerController.allowPreview = NO;
        pickerController.allowTakeVideo = NO;
        pickerController.allowPickingVideo = NO;
            pickerController.naviTitleColor=HEXC(0x232323);
        pickerController.naviTitleFont = Bfont17;
            pickerController.barItemTextColor=HEXC(0x232323);
            pickerController.barItemTextFont=font15;
        
        [uc presentViewController:pickerController animated:YES completion:nil];
    }
    else {
        
            //1.创建alertController实例
        FCAlertViewController *alert=[[FCAlertViewController alloc] initWithTitle:nil andSubTitle:@"请选择图片" andAlertType:FCAlertSheet];
        alert.contentView.hidden=YES;
            //3.将 AlertAction 添加到 AlertController中
        FCAlertAction *action3=[FCAlertAction actionWithTitle:@"手机相册" style:FCAlertActionCancel handler:^(FCAlertAction *action) {
                //        UIAlertAction *action3 = [UIAlertAction actionWithTitle:commonUseLocaChanged(@"common_im_handset_album",@"手机相册") style:0 handler:^(UIAlertAction * _Nonnull action) {
            TZImagePickerController *pickerController = [[TZImagePickerController alloc]initWithMaxImagesCount:self.maxChoiceImage-self.imageArr.count delegate:self];
            pickerController.isStatusBarDefault =YES;
            pickerController.naviTitleColor=HEXC(0x232323);
            pickerController.barItemTextColor=HEXC(0x232323);
            pickerController.barItemTextFont=font15;
            [uc presentViewController:pickerController animated:YES completion:nil];
        }];
        [alert addAction:action3];
        if(_takePhoto){//拍照
            FCAlertAction *action5=[FCAlertAction actionWithTitle:@"拍照" style:FCAlertActionCancel handler:^(FCAlertAction *action) {
//
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
                    pickerController.delegate = self;
                    pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [uc presentViewController:pickerController animated:YES completion:nil];
                }
            }];
             [alert addAction:action5];
        }
        

        
         FCAlertAction *cancel=[FCAlertAction actionWithTitle:@"取消" style:FCAlertActionDelete handler:nil];
         [alert addAction:cancel];
        [cancel setTitleColor:HEXC(0x646262) forState:UIControlStateNormal];
        [uc presentViewController:alert animated:YES completion:nil];
    }
    
    }

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos
{
    if (isSelectOriginalPhoto)
        {
        }
    if (!self.imageArr) {
        self.imageArr=[NSMutableArray new];
    }
    
    [self.imageArr addObjectsFromArray:photos];
    [self refreshImage];
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    WJCWeakSelf
    [picker dismissViewControllerAnimated:YES completion:nil];
    if (image) {
        if (!self.imageArr) {
            self.imageArr=[NSMutableArray new];
        }
        [weakSelf.imageArr addObject:image];
        [weakSelf refreshImage];
    }
}
-(void)TImgBLabelcloseBtnClickWithButton:(UIButton *)sender AndSuperView:(FCTImgBLabelView *)view{
    [self.imageArr removeObjectAtIndex:sender.tag];
    [self refreshImage];
}
-(void)TImgBLabelbottomTagLabelTap:(UITapGestureRecognizer *)sender AndSuperView:(FCTImgBLabelView *)view{
    self.choiceShowImgTag=sender.view.tag;
    [self refreshWithNoDelegateImage];
}
@end
